import torch
import torch.nn as nn
from bottleneck import Bottleneck

class GhostHornetResNet(nn.Module):
    def __init__(self, num_classes=100):
        super(GhostHornetResNet, self).__init__()
        self.in_planes = 64
        self.conv1 = nn.Sequential(
            nn.Conv2d(3, 64, kernel_size=3, stride=1, padding=1, bias=False),
            nn.BatchNorm2d(64),
            nn.ReLU()
        )
        self.layer1 = self._make_layer(64, 2, stride=1)
        self.layer2 = self._make_layer(128, 2, stride=2)
        self.layer3 = self._make_layer(256, 2, stride=2, use_hornet=True)
        self.layer4 = self._make_layer(512, 2, stride=2, use_hornet=True)
        self.avgpool = nn.AdaptiveAvgPool2d((1, 1))
        self.fc = nn.Linear(512, num_classes)

    def _make_layer(self, planes, blocks, stride, use_hornet=False):
        strides = [stride] + [1] * (blocks - 1)
        layers = []
        for s in strides:
            layers.append(Bottleneck(self.in_planes, planes, s, use_hornet))
            self.in_planes = planes
        return nn.Sequential(*layers)

    def forward(self, x):
        x = self.conv1(x)
        x = self.layer1(x)
        x = self.layer2(x)
        x = self.layer3(x)
        x = self.layer4(x)
        x = self.avgpool(x)
        x = torch.flatten(x, 1)
        x = self.fc(x)
        return x
